package com.yhb.controller.admin;

import com.yhb.conf.ResultCode;
import com.yhb.controller.vo.ArrayWrapperVo;
import com.yhb.model.Card;
import com.yhb.model.User;
import com.yhb.service.CardService;
import com.yhb.service.UserService;
import lombok.var;
import me.edzh.spring.bootstrap.annotations.NeedAuth;
import me.edzh.spring.bootstrap.controller.BaseController;
import me.edzh.spring.bootstrap.model.Response;
import me.edzh.spring.bootstrap.utils.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;


@RequestMapping("/admin/card")
@RestController
public class AdminCardController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private CardService cardService;

    @GetMapping("/user/{mobile}")
    @NeedAuth(User.A_ADMIN)
    public Response<ArrayWrapperVo<Card>> getUserCard(@PathVariable("mobile")String userMobile) {
        var u = userService.getUserByMobilePhone(userMobile);
        if (u == null) {
            throw new ServiceException(ResultCode.ERROR_TOAST_ERR, "用户不存在");
        }

        return Response.ok(new ArrayWrapperVo<>(cardService.getUserCard(u.getId())));
    }
}
